package com.yc.baihe.biz.impl;

import java.util.ArrayList;
import java.util.List;

import com.yc.baihe.beans.Story;
import com.yc.baihe.biz.StoryBiz;
import com.yc.baihe.dao.DBHelper;
import com.yc.baihe.dao.MD5Encryption;

public class StoryBizImpl implements StoryBiz {
	DBHelper db=new DBHelper();
	public List<Story> find(Integer pageNo, Integer pageSize) {
		String sql=null;
		List<Object> params=new ArrayList<Object>();
		if(pageNo==null || pageNo==0){
			sql="select * from story";
		}else{
			sql="select * from(select a.*,rownum rn from (select * from story order by s_num) a where rownum<=?) b where rn>?";
			params.add(pageNo*pageSize);
			params.add((pageNo-1)*pageSize);
		}
		return db.find(sql,params,Story.class);
	}
	
	public List<Story> RandomFind(Integer storypageNo) {
		String sql="select * from(select a.*,rownum rn from (select * from story where status=2 order by s_num) a where rownum<=?) b where rn>?";
		List<Object> params=new ArrayList<Object>();
		params.add(storypageNo+7);
		params.add(storypageNo);
		return db.find(sql, params,Story.class);
	}

	public int total() {
		String sql="select count(s_num) from story";
		List<List<String>> list=db.finds(sql, null);
		if(list!=null && list.size()>0){
			return Integer.parseInt(list.get(0).get(0));
		}
		return 0;
	}

	public List<Story> findByS_num(int S_num) {
		String sql="select * from story where s_num=?";
		List<Object> params=new ArrayList<Object>();
		params.add(S_num);
		return db.find(sql, params,Story.class);
	}

	public int updataTotaltickBys_num(int s_num,String totaltick) {
		String sql = "update story set totaltick=? where s_num=?";
		List<Object> params = new ArrayList<Object>();
		params.add(totaltick);
		params.add(s_num);
		return db.update(sql, params);
	}

	public int addStory(Story tf) {
		String sql="insert into story values(seq_story_s_num.nextval,?,?," +
				"null,null,'放荡不羁爱自由....',?," +
				"null,?,null,1,?,null,null)";
		List<Object> params=new ArrayList<Object>();
		params.add(tf.getBoy_id());
		params.add(tf.getGirl_id());
		params.add(tf.getMarriage_time());
		params.add(tf.getLoveTab());
		params.add(tf.getTimefrombh());
		
		return db.update(sql, params);
	}

	public int isExistBid(int boy_id) {
		String sql="select count(boy_id) from story where boy_id=?";
		List<Object> params=new ArrayList<Object>();
		params.add(boy_id);
		List<List<String>> list=db.finds(sql, params);
		if(list!=null && list.size()>0){
			return Integer.parseInt(list.get(0).get(0));
		}
		return 0;
	}

	public int updataStoryByBoy_id(int Boy_id, String title, String small_title, String content) {
		String sql="update story set title=?,small_title=?,story_content=? where boy_id=?";
		List<Object> params=new ArrayList<Object>();
		params.add(title);
		params.add(small_title);
		params.add(content);
		params.add(Boy_id);
		return db.update(sql, params);
	}

	public List<Story> findByBoy_id(int boy_id) {
		String sql="select * from story where boy_id=?";
		List<Object> params=new ArrayList<Object>();
		params.add(boy_id);
		return db.find(sql, params,Story.class);
	}

	public int updateStory(Story tf) {
		String sql="update story set status=1,girl_id=?,marriage_time=?,loveTab=?,timefrombh=? where boy_id=?";
		List<Object> params=new ArrayList<Object>();
		params.add(tf.getGirl_id());
		params.add(tf.getMarriage_time());
		params.add(tf.getLoveTab());
		params.add(tf.getTimefrombh());
		params.add(tf.getBoy_id());
		return db.update(sql, params);
	}

	public int updataPhotoByBoy_id(int boy_id, String photo) {
		String sql = "update story set two_photo=? where boy_id=?";
		List<Object> params = new ArrayList<Object>();
		params.add(photo);
		params.add(boy_id);
		return db.update(sql, params);
	}


}
